/*!
 * \file test_trader_adapter_basic.cpp
 * \project WonderTrader
 *
 * \author Generated
 * \date 2024/03/20
 * 
 * \brief TraderAdapter基础功能测试
 * 
 * \details 测试TraderAdapter类的基础功能，包括：
 *          - 适配器状态枚举测试
 *          - 持仓项结构测试
 *          - 风险参数结构测试
 *          - 基础构造和状态管理
 *          - 线程安全性测试
 *          - 性能特性测试
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>
#include <memory>
#include <boost/noncopyable.hpp>

// 包含必要的头文件
#include "../../src/WtCore/TraderAdapter.h"
#include "../../src/Includes/WTSDataDef.hpp"
#include "../../src/Includes/WTSVariant.hpp"

using namespace wtp;

/**
 * @class TraderAdapterBasicTest
 * @brief TraderAdapter基础功能测试类
 * 
 * @details 测试TraderAdapter的基础功能和数据结构
 */
class TraderAdapterBasicTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 测试设置
    }

    void TearDown() override {
        // 测试清理
    }
};

/**
 * @brief 测试适配器状态枚举值
 * 
 * @details 验证TraderAdapter::AdapterState枚举的各个状态值是否正确定义
 */
TEST_F(TraderAdapterBasicTest, TestAdapterStateEnums) {
    // 测试适配器状态枚举值
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_NOTLOGIN), 0);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_LOGINING), 1);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_LOGINED), 2);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_LOGINFAILED), 3);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_POSITION_QRYED), 4);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_ORDERS_QRYED), 5);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_TRADES_QRYED), 6);
    EXPECT_EQ(static_cast<int>(wtp::TraderAdapter::AS_ALLREADY), 7);
}

/**
 * @brief 测试持仓项结构
 * 
 * @details 验证TraderAdapter::PosItem结构的基本功能和数据完整性
 */
TEST_F(TraderAdapterBasicTest, TestPosItemStructure) {
    // 创建持仓项实例
    wtp::TraderAdapter::PosItem posItem;
    
    // 测试默认初始化
    EXPECT_EQ(posItem.l_newvol, 0.0);
    EXPECT_EQ(posItem.l_newavail, 0.0);
    EXPECT_EQ(posItem.l_prevol, 0.0);
    EXPECT_EQ(posItem.l_preavail, 0.0);
    EXPECT_EQ(posItem.s_newvol, 0.0);
    EXPECT_EQ(posItem.s_newavail, 0.0);
    EXPECT_EQ(posItem.s_prevol, 0.0);
    EXPECT_EQ(posItem.s_preavail, 0.0);
    
    // 测试数据设置
    posItem.l_newvol = 100.0;
    posItem.l_newavail = 90.0;
    posItem.l_prevol = 50.0;
    posItem.l_preavail = 50.0;
    posItem.s_newvol = 30.0;
    posItem.s_newavail = 25.0;
    posItem.s_prevol = 20.0;
    posItem.s_preavail = 20.0;
    
    // 验证数据设置
    EXPECT_EQ(posItem.l_newvol, 100.0);
    EXPECT_EQ(posItem.l_newavail, 90.0);
    EXPECT_EQ(posItem.l_prevol, 50.0);
    EXPECT_EQ(posItem.l_preavail, 50.0);
    EXPECT_EQ(posItem.s_newvol, 30.0);
    EXPECT_EQ(posItem.s_newavail, 25.0);
    EXPECT_EQ(posItem.s_prevol, 20.0);
    EXPECT_EQ(posItem.s_preavail, 20.0);
    
    // 测试持仓计算方法
    EXPECT_EQ(posItem.total_pos(true), 150.0);   // 多头总持仓: 100 + 50
    EXPECT_EQ(posItem.total_pos(false), 50.0);   // 空头总持仓: 30 + 20
    EXPECT_EQ(posItem.avail_pos(true), 140.0);   // 多头可用: 90 + 50
    EXPECT_EQ(posItem.avail_pos(false), 45.0);   // 空头可用: 25 + 20
}

/**
 * @brief 测试风险参数结构
 * 
 * @details 验证TraderAdapter::RiskParams结构的基本功能
 */
TEST_F(TraderAdapterBasicTest, TestRiskParamsStructure) {
    // 创建风险参数实例
    wtp::TraderAdapter::RiskParams riskParams;
    
    // 测试默认初始化
    EXPECT_EQ(riskParams._order_times_boundary, 0);
    EXPECT_EQ(riskParams._order_stat_timespan, 0);
    EXPECT_EQ(riskParams._order_total_limits, 0);
    EXPECT_EQ(riskParams._cancel_times_boundary, 0);
    EXPECT_EQ(riskParams._cancel_stat_timespan, 0);
    EXPECT_EQ(riskParams._cancel_total_limits, 0);
    
    // 测试数据设置
    riskParams._order_times_boundary = 100;
    riskParams._order_stat_timespan = 60;
    riskParams._order_total_limits = 1000;
    riskParams._cancel_times_boundary = 10;
    riskParams._cancel_stat_timespan = 30;
    riskParams._cancel_total_limits = 50;
    
    // 验证数据设置
    EXPECT_EQ(riskParams._order_times_boundary, 100);
    EXPECT_EQ(riskParams._order_stat_timespan, 60);
    EXPECT_EQ(riskParams._order_total_limits, 1000);
    EXPECT_EQ(riskParams._cancel_times_boundary, 10);
    EXPECT_EQ(riskParams._cancel_stat_timespan, 30);
    EXPECT_EQ(riskParams._cancel_total_limits, 50);
    
    // 测试风险控制逻辑
    bool order_risk_check = (riskParams._order_times_boundary > 0) && 
                           (riskParams._order_stat_timespan > 0);
    EXPECT_TRUE(order_risk_check);
    
    bool cancel_risk_check = (riskParams._cancel_times_boundary > 0) && 
                            (riskParams._cancel_stat_timespan > 0);
    EXPECT_TRUE(cancel_risk_check);
}

/**
 * @brief 测试适配器基础构造
 * 
 * @details 验证TraderAdapter的基础构造函数和初始状态
 */
TEST_F(TraderAdapterBasicTest, TestBasicConstruction) {
    // 测试默认构造
    {
        wtp::TraderAdapter adapter;
        EXPECT_EQ(adapter.state(), wtp::TraderAdapter::AS_NOTLOGIN);
    }
    
    // 测试带参数构造
    {
        wtp::TraderAdapter adapter(nullptr);
        EXPECT_EQ(adapter.state(), wtp::TraderAdapter::AS_NOTLOGIN);
    }
}

/**
 * @brief 测试持仓项边界条件
 * 
 * @details 测试PosItem在极值和边界条件下的行为
 */
TEST_F(TraderAdapterBasicTest, TestPosItemBoundaryConditions) {
    wtp::TraderAdapter::PosItem posItem;
    
    // 测试负值处理
    posItem.l_newvol = -100.0;
    posItem.l_newavail = -10.0;
    EXPECT_LT(posItem.l_newvol, 0.0);
    EXPECT_LT(posItem.l_newavail, 0.0);
    
    // 测试极大值
    posItem.l_newvol = 1e10;
    posItem.l_prevol = 1e6;
    EXPECT_GT(posItem.l_newvol, 1e9);
    EXPECT_GT(posItem.l_prevol, 1e5);
    
    // 测试零值边界
    posItem.l_newvol = 0.0;
    posItem.l_newavail = 0.0;
    posItem.l_prevol = 0.0;
    posItem.l_preavail = 0.0;
    EXPECT_EQ(posItem.total_pos(true), 0.0);
    EXPECT_EQ(posItem.avail_pos(true), 0.0);
}

/**
 * @brief 测试线程安全性
 * 
 * @details 测试TraderAdapter相关数据结构在多线程环境下的安全性
 */
TEST_F(TraderAdapterBasicTest, TestThreadSafety) {
    wtp::TraderAdapter::PosItem shared_pos_item;
    std::atomic<bool> test_completed{false};
    std::vector<std::thread> threads;
    
    // 启动多个线程同时访问持仓项
    for (int i = 0; i < 4; ++i) {
        threads.emplace_back([&shared_pos_item, &test_completed, i]() {
            while (!test_completed.load()) {
                wtp::TraderAdapter::PosItem local_item;
                local_item.l_newvol = i * 10.0;
                local_item.l_newavail = i * 8.0;
                local_item.l_prevol = i * 5.0;
                local_item.l_preavail = i * 5.0;
                
                // 模拟数据访问
                double total_long = local_item.total_pos(true);
                double avail_long = local_item.avail_pos(true);
                EXPECT_GE(total_long, 0.0);
                EXPECT_GE(avail_long, 0.0);
                
                std::this_thread::sleep_for(std::chrono::microseconds(1));
            }
        });
    }
    
    // 运行测试一段时间
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    test_completed.store(true);
    
    // 等待所有线程完成
    for (auto& thread : threads) {
        thread.join();
    }
    
    EXPECT_TRUE(test_completed.load());
}

/**
 * @brief 测试性能特性
 * 
 * @details 测试TraderAdapter相关操作的性能特性
 */
TEST_F(TraderAdapterBasicTest, TestPerformanceCharacteristics) {
    const int iterations = 10000;
    
    auto start_time = std::chrono::high_resolution_clock::now();
    
    // 测试持仓项创建和计算性能
    for (int i = 0; i < iterations; ++i) {
        wtp::TraderAdapter::PosItem posItem;
        posItem.l_newvol = i * 1.0;
        posItem.l_newavail = i * 0.9;
        posItem.l_prevol = i * 0.5;
        posItem.l_preavail = i * 0.5;
        
        // 执行一些计算
        double total_long = posItem.total_pos(true);
        double avail_long = posItem.avail_pos(true);
        
        // 避免编译器优化
        volatile double result = total_long + avail_long;
        (void)result;
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
        end_time - start_time);
    
    // 验证性能在合理范围内（应该在毫秒级别完成）
    EXPECT_LT(duration.count(), 100000); // 小于100毫秒
    
    // 输出性能信息
    std::cout << "Performance test: " << iterations 
              << " iterations completed in " << duration.count() 
              << " microseconds" << std::endl;
}

/**
 * @brief 测试数据一致性
 * 
 * @details 验证持仓项数据的一致性和完整性
 */
TEST_F(TraderAdapterBasicTest, TestDataConsistency) {
    wtp::TraderAdapter::PosItem posItem;
    
    // 设置一组一致的数据
    posItem.l_newvol = 1000.0;
    posItem.l_newavail = 900.0;
    posItem.l_prevol = 500.0;
    posItem.l_preavail = 500.0;
    posItem.s_newvol = 200.0;
    posItem.s_newavail = 180.0;
    posItem.s_prevol = 100.0;
    posItem.s_preavail = 100.0;
    
    // 验证数据一致性
    double total_long = posItem.total_pos(true);
    double total_short = posItem.total_pos(false);
    double avail_long = posItem.avail_pos(true);
    double avail_short = posItem.avail_pos(false);
    
    EXPECT_EQ(total_long, 1500.0);   // 1000 + 500
    EXPECT_EQ(total_short, 300.0);   // 200 + 100
    EXPECT_EQ(avail_long, 1400.0);   // 900 + 500
    EXPECT_EQ(avail_short, 280.0);   // 180 + 100
    
    // 测试可用持仓不应超过总持仓
    EXPECT_LE(avail_long, total_long);
    EXPECT_LE(avail_short, total_short);
    
    // 测试净持仓计算
    double net_position = total_long - total_short;
    EXPECT_EQ(net_position, 1200.0);
} 